package com.team5101.lha.team5101.web.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.team5101.dto.PostbarDTO;
import com.team5101.entity.TPostBar;
import com.team5101.postbar.api.IPostBarService;
import com.team5101.vo.ResultBean;
import io.lettuce.core.models.role.RedisSlaveInstance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/api/postbar")
public class PostBarController {


    @Reference
    public IPostBarService iPostBarService;

    //test
    @RequestMapping("/pbarlist/{id}")
    @ResponseBody
    public String getAllpostbar(@PathVariable Integer id){

        TPostBar tPostBar = iPostBarService.selectByPrimaryKey( id.longValue());
        return tPostBar.toString();
    }



    //用户发帖
    @RequestMapping("/createpbar")
    @ResponseBody
    public ResultBean createBar(@RequestBody PostbarDTO postbarDTO){

       // System.out.println(postbarDTO);
        TPostBar tPostBar = new TPostBar();
        tPostBar.setPbContext(postbarDTO.getPbContext());
        tPostBar.setPbCreatetime(postbarDTO.getPbCreateTime());
        tPostBar.setPbImageMain(postbarDTO.getPbImageMain());
        tPostBar.setPbImgaeBak(postbarDTO.getPbImgaeBak());
        tPostBar.setPbPrice(postbarDTO.getPbPrice());
        tPostBar.setPbSign(postbarDTO.getPbSign());
        tPostBar.setPbUid(postbarDTO.getPbUid().longValue());

        int i = iPostBarService.insertSelective(tPostBar);

        if(i>=0)
            return new ResultBean().success(null,"发帖成功");
        else
            return new ResultBean().error(null,"发帖失败");
    }
    ///未test---------------------------------------------

    //查用户贴
    @RequestMapping("/showpbarbyid")
    @ResponseBody
    public ResultBean showBarById(@RequestBody PostbarDTO postbarDTO){

        List<TPostBar> data = iPostBarService.getListBydata(postbarDTO);
        if(data!=null)
            return new ResultBean().success(data,"查询成功");
        else
            return new ResultBean().error(null,"查询失败");
    }

    //社区贴所有信息展示接口
    @RequestMapping("/pbarlist")
    @ResponseBody
    public ResultBean showlist(){
        PostbarDTO postbarDTO=new  PostbarDTO();
        postbarDTO.setPbId(null);
        postbarDTO.setPbUid(null);
        List<TPostBar> data = iPostBarService.getListBydata(postbarDTO);
        if(data!=null)
            return new ResultBean().success(data,"查询成功");
        else
            return new ResultBean().error(null,"查询失败");
    }

    //修改帖子信息
    @RequestMapping("/updatepbar")
    @ResponseBody
    public ResultBean updatedata(@RequestBody PostbarDTO postbarDTO){
        TPostBar tPostBar = new TPostBar();
        tPostBar.setPbId(postbarDTO.getPbId().longValue());
        tPostBar.setPbContext(postbarDTO.getPbContext());
        tPostBar.setPbCreatetime(postbarDTO.getPbCreateTime());
        tPostBar.setPbImageMain(postbarDTO.getPbImageMain());
        tPostBar.setPbImgaeBak(postbarDTO.getPbImgaeBak());
        tPostBar.setPbPrice(postbarDTO.getPbPrice());
        tPostBar.setPbSign(postbarDTO.getPbSign());
        tPostBar.setPbUid(postbarDTO.getPbUid().longValue());

        int i = iPostBarService.updateByPrimaryKeySelective(tPostBar);

        if(i>=0)
            return new ResultBean().success(null,"修改成功");
        else
            return new ResultBean().error(null,"修改失败");

    }


    //删除贴
    @RequestMapping("/deletepbarbyid")
    @ResponseBody
    public ResultBean deletedata(@RequestBody  PostbarDTO postbarDTO){
        int i = iPostBarService.deleteByKey(postbarDTO);
        if(i>=0)
            return new ResultBean().success(null,"删除成功");
        else
            return new ResultBean().error(null,"删除失败");
    }
}
